package org.ehe.business.bom.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.ehe.business.bom.domain.BomChangeLog;
import org.ehe.business.bom.domain.bo.BomChangeLogBo;
import org.ehe.business.bom.domain.vo.BomChangeLogVO;
import org.ehe.business.bom.service.BomChangeLogService;
import org.ehe.common.core.domain.R;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * BOM变更记录管理
 * @author :zhangnn
 * @className :BomChangeLogController
 * @description: BOM变更记录控制器
 * @date 2025-08-05 14:08:51
 */
@RestController
@RequestMapping("/bom/change")
@RequiredArgsConstructor
public class BomChangeLogController {

    private final BomChangeLogService bomChangeLogService;

    /**
     * 分页列表
     * @param bo
     * @param pageQuery
     * @return
     */
    @PostMapping("/page")
    public TableDataInfo<BomChangeLogVO> pageChangeLogs(@RequestBody BomChangeLogBo bo, PageQuery pageQuery) {
        return bomChangeLogService.pageChangeLogs(bo, pageQuery);
    }

    /**
     * 创建变更记录
     * @param dto
     * @return
     */
    @Log(title = "创建BOM变更记录", linkType = "bomChangeLog", businessType = BusinessType.INSERT, idField = "id", nameField = "changeReason")
    @PostMapping("/create")
    public R<Long> createChangeRequest(@Valid @RequestBody BomChangeLog dto) {
        BomChangeLog changeLog = bomChangeLogService.createChangeRequest(dto);
        return R.ok("变更记录创建成功", changeLog.getId());
    }

    /**
     * 提交变更审批
     * @param changeLogId
     * @param workflowInstanceId
     * @return
     */
    @Log(title = "提交BOM变更审批", linkType = "bomChangeLog", businessType = BusinessType.UPDATE, idField = "changeLogId")
    @PostMapping("/submit/{changeLogId}")
    public R<Boolean> submitChangeForApproval(@PathVariable Long changeLogId,
                                              @RequestParam Long workflowInstanceId) {
        bomChangeLogService.submitChangeForApproval(changeLogId, workflowInstanceId);
        return R.ok("变更提交审批成功");
    }

    /**
     * 审批通过变更请求
     * @param changeLogId
     * @param approverId
     * @param approverName
     * @param approvalComments
     * @return
     */
    @Log(title = "审批通过BOM变更", linkType = "bomChangeLog", businessType = BusinessType.UPDATE, idField = "changeLogId")
    @PostMapping("/approve/{changeLogId}")
    public R<Boolean> approveChange(@PathVariable Long changeLogId,
                                    @RequestParam Long approverId,
                                    @RequestParam String approverName,
                                    @RequestParam(required = false) String approvalComments) {
        bomChangeLogService.approveChange(changeLogId, approverId, approverName, approvalComments);
        return R.ok("变更审批通过成功");
    }

    /**
     * 拒绝变更请求
     * @param changeLogId
     * @param approverId
     * @param approverName
     * @param rejectReason
     * @return
     */
    @Log(title = "拒绝BOM变更", linkType = "bomChangeLog", businessType = BusinessType.UPDATE, idField = "changeLogId")
    @PostMapping("/reject/{changeLogId}")
    public R<Boolean> rejectChange(@PathVariable Long changeLogId,
                                   @RequestParam Long approverId,
                                   @RequestParam String approverName,
                                   @RequestParam String rejectReason) {
        bomChangeLogService.rejectChange(changeLogId, approverId, approverName, rejectReason);
        return R.ok("变更拒绝成功");
    }

    /**
     * 实施变更
     * @param changeLogId
     * @return
     */
    @Log(title = "实施BOM变更", linkType = "bomChangeLog", businessType = BusinessType.UPDATE, idField = "changeLogId")
    @PostMapping("/implement/{changeLogId}")
    public R<Boolean> implementChange(@PathVariable Long changeLogId) {
        bomChangeLogService.implementChange(changeLogId);
        return R.ok("变更实施成功");
    }

    /**
     * 撤销变更请求
     * @param changeLogId
     * @param applicantId
     * @return
     */
    @Log(title = "撤销BOM变更请求", linkType = "bomChangeLog", businessType = BusinessType.DELETE, idField = "changeLogId")
    @PostMapping("/withdraw/{changeLogId}")
    public R<Boolean> withdrawChange(@PathVariable Long changeLogId,
                                     @RequestParam Long applicantId) {
        bomChangeLogService.withdrawChange(changeLogId, applicantId);
        return R.ok("变更请求撤销成功");
    }

    /**
     * 查看变更详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public R<BomChangeLogVO> getChangeLogDetail(@PathVariable Long id) {
        BomChangeLogVO result = bomChangeLogService.getChangeLogDetail(id);
        return R.ok(result);
    }

    /**
     * 批量审批变更请求
     * @param changeLogIds
     * @param approverId
     * @param approverName
     * @param batchComments
     * @return
     */
    @Log(title = "批量审批BOM变更", linkType = "bomChangeLog", businessType = BusinessType.UPDATE)
    @PostMapping("/batch-approve")
    public R<Boolean> batchApproveChanges(@RequestBody List<Long> changeLogIds,
                                          @RequestParam Long approverId,
                                          @RequestParam String approverName,
                                          @RequestParam(required = false) String batchComments) {
        bomChangeLogService.batchApproveChanges(changeLogIds, approverId, approverName, batchComments);
        return R.ok("批量审批完成");
    }

    /**
     * 批量拒绝变更请求
     * @param changeLogIds
     * @param approverId
     * @param approverName
     * @param rejectReason
     * @return
     */
    @Log(title = "批量拒绝BOM变更", linkType = "bomChangeLog", businessType = BusinessType.UPDATE)
    @PostMapping("/batch-reject")
    public R<Boolean> batchRejectChanges(@RequestBody List<Long> changeLogIds,
                                         @RequestParam Long approverId,
                                         @RequestParam String approverName,
                                         @RequestParam String rejectReason) {
        bomChangeLogService.batchRejectChanges(changeLogIds, approverId, approverName, rejectReason);
        return R.ok("批量拒绝完成");
    }

    /**
     * 根据BOM ID查询变更记录
     * @param bomId
     * @return
     */
    @GetMapping("/by-bom/{bomId}")
    public R<List<BomChangeLogVO>> getChangeLogsByBomId(@PathVariable Long bomId) {
        List<BomChangeLog> changeLogs = bomChangeLogService.getChangeLogsByBomId(bomId);
        List<BomChangeLogVO> result = changeLogs.stream()
            .map(this::convertToVO)
            .toList();
        return R.ok(result);
    }

    /**
     * 根据变更状态查询变更记录
     * @param changeStatus
     * @return
     */
    @GetMapping("/by-status/{changeStatus}")
    public R<List<BomChangeLogVO>> getChangeLogsByStatus(@PathVariable Integer changeStatus) {
        List<BomChangeLog> changeLogs = bomChangeLogService.getChangeLogsByStatus(changeStatus);
        List<BomChangeLogVO> result = changeLogs.stream()
            .map(this::convertToVO)
            .toList();
        return R.ok(result);
    }

    /**
     * 获取变更统计信息
     * @param bomId
     * @return
     */
    @GetMapping("/statistics/{bomId}")
    public R<Map<String, Object>> getChangeStatistics(@PathVariable Long bomId) {
        Map<String, Object> statistics = bomChangeLogService.getChangeStatistics(bomId);
        return R.ok(statistics);
    }




    /**
     * 生成变更影响分析报告
     * @param changeLogId
     * @return
     */
    @GetMapping("/impact-analysis/{changeLogId}")
    public R<String> generateImpactAnalysis(@PathVariable Long changeLogId) {
        BomChangeLog changeLog = bomChangeLogService.getById(changeLogId);
        if (changeLog == null) {
            return R.fail("变更记录不存在");
        }
        String analysis = bomChangeLogService.generateImpactAnalysis(changeLog);
        return R.ok(analysis);
    }


    /**
     * 创建变更内容JSON
     * @param changeData
     * @return
     */
    @PostMapping("/create-change-content")
    public R<String> createChangeContentJson(@RequestBody Map<String, Object> changeData) {
        String json = bomChangeLogService.createChangeContentJson(changeData);
        return R.ok(json);
    }

    /**
     * 转换为VO对象的辅助方法
     * @param changeLog
     * @return
     */
    private BomChangeLogVO convertToVO(BomChangeLog changeLog) {
        // 这里需要根据实际的BomChangeLogVO类来实现转换逻辑
        // 假设有相应的转换方法或工具类
        BomChangeLogVO vo = new BomChangeLogVO();
        // 复制属性...
        return vo;
    }
}
